<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Thresholding Operations using inRange</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Thresholding Operations using inRange </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../db/d8e/tutorial_threshold.html">Basic Thresholding Operations</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../d4/dbd/tutorial_filter_2d.html">Making your own linear filters!</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Lorena García </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">Rishiraj Surti </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to:</p>
<ul>
<li>Perform basic thresholding operations using OpenCV <a class="el" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv::inRange</a> function.</li>
<li>Detect an object based on the range of pixel values in the HSV colorspace.</li>
</ul>
<h2>Theory </h2>
<ul>
<li>In the previous tutorial, we learnt how to perform thresholding using <a class="el" href="../../d7/d1b/group__imgproc__misc.html#gae8a4a146d1ca78c626a53577199e9c57">cv::threshold</a> function.</li>
<li>In this tutorial, we will learn how to do it using <a class="el" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv::inRange</a> function.</li>
<li>The concept remains the same, but now we add a range of pixel values we need.</li>
</ul>
<h2>HSV colorspace </h2>
<p><a href="https://en.wikipedia.org/wiki/HSL_and_HSV">HSV</a> (hue, saturation, value) colorspace is a model to represent the colorspace similar to the RGB color model. Since the hue channel models the color type, it is very useful in image processing tasks that need to segment objects based on its color. Variation of the saturation goes from unsaturated to represent shades of gray and fully saturated (no white component). Value channel describes the brightness or the intensity of the color. Next image shows the HSV cylinder.</p>
<div class="image">
<img src="../../Threshold_inRange_HSV_colorspace.jpg" alt="Threshold_inRange_HSV_colorspace.jpg"/>
<div class="caption">
By SharkDderivative work: SharkD [CC BY-SA 3.0 or GFDL], via Wikimedia Commons</div></div>
<p> Since colors in the RGB colorspace are coded using the three channels, it is more difficult to segment an object in the image based on its color.</p>
<div class="image">
<img src="../../Threshold_inRange_RGB_colorspace.jpg" alt="Threshold_inRange_RGB_colorspace.jpg"/>
<div class="caption">
By SharkD [GFDL or CC BY-SA 4.0], from Wikimedia Commons</div></div>
<p> Formulas used to convert from one colorspace to another colorspace using <a class="el" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv::cvtColor</a> function are described in <a class="el" href="../../de/d25/imgproc_color_conversions.html">Color conversions</a></p>
<h2>Code </h2>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/cpp/tutorial_code/ImgProc/Threshold_inRange.cpp">here</a> </p><div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> max_value_H = 360/2;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> max_value = 255;</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> window_capture_name = <span class="stringliteral">&quot;Video Capture&quot;</span>;</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> window_detection_name = <span class="stringliteral">&quot;Object Detection&quot;</span>;</div><div class="line"><span class="keywordtype">int</span> low_H = 0, low_S = 0, low_V = 0;</div><div class="line"><span class="keywordtype">int</span> high_H = max_value_H, high_S = max_value, high_V = max_value;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_low_H_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    low_H = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a>(high_H-1, low_H);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;Low H&quot;</span>, window_detection_name, low_H);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_high_H_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    high_H = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(high_H, low_H+1);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;High H&quot;</span>, window_detection_name, high_H);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_low_S_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    low_S = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a>(high_S-1, low_S);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;Low S&quot;</span>, window_detection_name, low_S);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_high_S_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    high_S = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(high_S, low_S+1);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;High S&quot;</span>, window_detection_name, high_S);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_low_V_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    low_V = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a>(high_V-1, low_V);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;Low V&quot;</span>, window_detection_name, low_V);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_high_V_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    high_V = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(high_V, low_V+1);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;High V&quot;</span>, window_detection_name, high_V);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line">    <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap(argc &gt; 1 ? atoi(argv[1]) : 0);</div><div class="line"></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(window_capture_name);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(window_detection_name);</div><div class="line"></div><div class="line">    <span class="comment">// Trackbars to set thresholds for HSV values</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low H&quot;</span>, window_detection_name, &amp;low_H, max_value_H, on_low_H_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High H&quot;</span>, window_detection_name, &amp;high_H, max_value_H, on_high_H_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low S&quot;</span>, window_detection_name, &amp;low_S, max_value, on_low_S_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High S&quot;</span>, window_detection_name, &amp;high_S, max_value, on_high_S_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low V&quot;</span>, window_detection_name, &amp;low_V, max_value, on_low_V_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High V&quot;</span>, window_detection_name, &amp;high_V, max_value, on_high_V_thresh_trackbar);</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame, frame_HSV, frame_threshold;</div><div class="line">    <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line">        cap &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span>(frame.empty())</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// Convert from BGR to HSV colorspace</span></div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame, frame_HSV, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">        <span class="comment">// Detect the object based on HSV Range Values</span></div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(frame_HSV, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(low_H, low_S, low_V), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(high_H, high_S, high_V), frame_threshold);</div><div class="line"></div><div class="line">        <span class="comment">// Show the frames</span></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(window_capture_name, frame);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(window_detection_name, frame_threshold);</div><div class="line"></div><div class="line">        <span class="keywordtype">char</span> key = (char) <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(30);</div><div class="line">        <span class="keywordflow">if</span> (key == <span class="charliteral">&#39;q&#39;</span> || key == 27)</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/java/tutorial_code/ImgProc/threshold_inRange/ThresholdInRange.java">here</a> </p><div class="fragment"><div class="line"><span class="keyword">import</span> java.awt.BorderLayout;</div><div class="line"><span class="keyword">import</span> java.awt.Container;</div><div class="line"><span class="keyword">import</span> java.awt.Image;</div><div class="line"><span class="keyword">import</span> java.awt.event.WindowAdapter;</div><div class="line"><span class="keyword">import</span> java.awt.event.WindowEvent;</div><div class="line"><span class="keyword">import</span> java.util.List;</div><div class="line"></div><div class="line"><span class="keyword">import</span> javax.swing.BoxLayout;</div><div class="line"><span class="keyword">import</span> javax.swing.ImageIcon;</div><div class="line"><span class="keyword">import</span> javax.swing.JFrame;</div><div class="line"><span class="keyword">import</span> javax.swing.JLabel;</div><div class="line"><span class="keyword">import</span> javax.swing.JPanel;</div><div class="line"><span class="keyword">import</span> javax.swing.JSlider;</div><div class="line"><span class="keyword">import</span> javax.swing.SwingWorker;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeEvent;</div><div class="line"><span class="keyword">import</span> javax.swing.event.ChangeListener;</div><div class="line"></div><div class="line"><span class="keyword">import</span> org.opencv.core.Core;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Mat;</div><div class="line"><span class="keyword">import</span> org.opencv.core.Scalar;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"><span class="keyword">import</span> org.opencv.videoio.VideoCapture;</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>ThresholdInRange {</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">int</span> MAX_VALUE = 255;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keywordtype">int</span> MAX_VALUE_H = 360/2;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> WINDOW_NAME = <span class="stringliteral">&quot;Thresholding Operations using inRange demo&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> LOW_H_NAME = <span class="stringliteral">&quot;Low H&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> LOW_S_NAME = <span class="stringliteral">&quot;Low S&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> LOW_V_NAME = <span class="stringliteral">&quot;Low V&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> HIGH_H_NAME = <span class="stringliteral">&quot;High H&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> HIGH_S_NAME = <span class="stringliteral">&quot;High S&quot;</span>;</div><div class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> HIGH_V_NAME = <span class="stringliteral">&quot;High V&quot;</span>;</div><div class="line">    <span class="keyword">private</span> JSlider sliderLowH;</div><div class="line">    <span class="keyword">private</span> JSlider sliderHighH;</div><div class="line">    <span class="keyword">private</span> JSlider sliderLowS;</div><div class="line">    <span class="keyword">private</span> JSlider sliderHighS;</div><div class="line">    <span class="keyword">private</span> JSlider sliderLowV;</div><div class="line">    <span class="keyword">private</span> JSlider sliderHighV;</div><div class="line">    <span class="keyword">private</span> VideoCapture cap;</div><div class="line">    <span class="keyword">private</span> Mat matFrame = <span class="keyword">new</span> Mat();</div><div class="line">    <span class="keyword">private</span> JFrame frame;</div><div class="line">    <span class="keyword">private</span> JLabel imgCaptureLabel;</div><div class="line">    <span class="keyword">private</span> JLabel imgDetectionLabel;</div><div class="line">    <span class="keyword">private</span> CaptureTask captureTask;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> ThresholdInRange(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="keywordtype">int</span> cameraDevice = 0;</div><div class="line">        <span class="keywordflow">if</span> (args.length &gt; 0) {</div><div class="line">            cameraDevice = Integer.parseInt(args[0]);</div><div class="line">        }</div><div class="line">        cap = <span class="keyword">new</span> VideoCapture(cameraDevice);</div><div class="line">        <span class="keywordflow">if</span> (!cap.isOpened()) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot open camera: &quot;</span> + cameraDevice);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line">        <span class="keywordflow">if</span> (!cap.read(matFrame)) {</div><div class="line">            System.err.println(<span class="stringliteral">&quot;Cannot read camera stream.&quot;</span>);</div><div class="line">            System.exit(0);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// Create and set up the window.</span></div><div class="line">        frame = <span class="keyword">new</span> JFrame(WINDOW_NAME);</div><div class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div><div class="line">        frame.addWindowListener(<span class="keyword">new</span> WindowAdapter() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> windowClosing(WindowEvent windowEvent) {</div><div class="line">                captureTask.cancel(<span class="keyword">true</span>);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        <span class="comment">// Set up the content pane.</span></div><div class="line">        Image img = HighGui.toBufferedImage(matFrame);</div><div class="line">        addComponentsToPane(frame.getContentPane(), img);</div><div class="line">        <span class="comment">// Use the content pane&#39;s default BorderLayout. No need for</span></div><div class="line">        <span class="comment">// setLayout(new BorderLayout());</span></div><div class="line">        <span class="comment">// Display the window.</span></div><div class="line">        frame.pack();</div><div class="line">        frame.setVisible(<span class="keyword">true</span>);</div><div class="line"></div><div class="line">        captureTask = <span class="keyword">new</span> CaptureTask();</div><div class="line">        captureTask.execute();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">class </span>CaptureTask <span class="keyword">extends</span> SwingWorker&lt;Void, Mat&gt; {</div><div class="line">        @Override</div><div class="line">        <span class="keyword">protected</span> Void doInBackground() {</div><div class="line">            Mat matFrame = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">            <span class="keywordflow">while</span> (!isCancelled()) {</div><div class="line">                <span class="keywordflow">if</span> (!cap.read(matFrame)) {</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">                publish(matFrame.clone());</div><div class="line">            }</div><div class="line">            <span class="keywordflow">return</span> null;</div><div class="line">        }</div><div class="line"></div><div class="line">        @Override</div><div class="line">        <span class="keyword">protected</span> <span class="keywordtype">void</span> process(List&lt;Mat&gt; frames) {</div><div class="line">            Mat frame = frames.get(frames.size() - 1);</div><div class="line">            Mat frameHSV = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_BGR2HSV);</div><div class="line">            Mat thresh = <span class="keyword">new</span> Mat();</div><div class="line">            Core.inRange(frameHSV, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(sliderLowH.getValue(), sliderLowS.getValue(), sliderLowV.getValue()),</div><div class="line">                    <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(sliderHighH.getValue(), sliderHighS.getValue(), sliderHighV.getValue()), thresh);</div><div class="line">            update(frame, thresh);</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> addComponentsToPane(Container pane, Image img) {</div><div class="line">        <span class="keywordflow">if</span> (!(pane.getLayout() instanceof BorderLayout)) {</div><div class="line">            pane.add(<span class="keyword">new</span> JLabel(<span class="stringliteral">&quot;Container doesn&#39;t use BorderLayout!&quot;</span>));</div><div class="line">            <span class="keywordflow">return</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        JPanel sliderPanel = <span class="keyword">new</span> JPanel();</div><div class="line">        sliderPanel.setLayout(<span class="keyword">new</span> BoxLayout(sliderPanel, BoxLayout.PAGE_AXIS));</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_H_NAME));</div><div class="line">        sliderLowH = <span class="keyword">new</span> JSlider(0, MAX_VALUE_H, 0);</div><div class="line">        sliderLowH.setMajorTickSpacing(50);</div><div class="line">        sliderLowH.setMinorTickSpacing(10);</div><div class="line">        sliderLowH.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowH.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowH);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_H_NAME));</div><div class="line">        sliderHighH = <span class="keyword">new</span> JSlider(0, MAX_VALUE_H, MAX_VALUE_H);</div><div class="line">        sliderHighH.setMajorTickSpacing(50);</div><div class="line">        sliderHighH.setMinorTickSpacing(10);</div><div class="line">        sliderHighH.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighH.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighH);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_S_NAME));</div><div class="line">        sliderLowS = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderLowS.setMajorTickSpacing(50);</div><div class="line">        sliderLowS.setMinorTickSpacing(10);</div><div class="line">        sliderLowS.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowS.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowS);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_S_NAME));</div><div class="line">        sliderHighS = <span class="keyword">new</span> JSlider(0, MAX_VALUE, MAX_VALUE);</div><div class="line">        sliderHighS.setMajorTickSpacing(50);</div><div class="line">        sliderHighS.setMinorTickSpacing(10);</div><div class="line">        sliderHighS.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighS.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighS);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_V_NAME));</div><div class="line">        sliderLowV = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderLowV.setMajorTickSpacing(50);</div><div class="line">        sliderLowV.setMinorTickSpacing(10);</div><div class="line">        sliderLowV.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowV.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowV);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_V_NAME));</div><div class="line">        sliderHighV = <span class="keyword">new</span> JSlider(0, MAX_VALUE, MAX_VALUE);</div><div class="line">        sliderHighV.setMajorTickSpacing(50);</div><div class="line">        sliderHighV.setMinorTickSpacing(10);</div><div class="line">        sliderHighV.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighV.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighV);</div><div class="line"></div><div class="line">        sliderLowH.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valH = Math.min(sliderHighH.getValue()-1, source.getValue());</div><div class="line">                sliderLowH.setValue(valH);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        sliderHighH.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valH = Math.max(source.getValue(), sliderLowH.getValue()+1);</div><div class="line">                sliderHighH.setValue(valH);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        sliderLowS.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valS = Math.min(sliderHighS.getValue()-1, source.getValue());</div><div class="line">                sliderLowS.setValue(valS);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        sliderHighS.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valS = Math.max(source.getValue(), sliderLowS.getValue()+1);</div><div class="line">                sliderHighS.setValue(valS);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        sliderLowV.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valV = Math.min(sliderHighV.getValue()-1, source.getValue());</div><div class="line">                sliderLowV.setValue(valV);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        sliderHighV.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valV = Math.max(source.getValue(), sliderLowV.getValue()+1);</div><div class="line">                sliderHighV.setValue(valV);</div><div class="line">            }</div><div class="line">        });</div><div class="line"></div><div class="line">        pane.add(sliderPanel, BorderLayout.PAGE_START);</div><div class="line">        JPanel framePanel = <span class="keyword">new</span> JPanel();</div><div class="line">        imgCaptureLabel = <span class="keyword">new</span> JLabel(<span class="keyword">new</span> ImageIcon(img));</div><div class="line">        framePanel.add(imgCaptureLabel);</div><div class="line">        imgDetectionLabel = <span class="keyword">new</span> JLabel(<span class="keyword">new</span> ImageIcon(img));</div><div class="line">        framePanel.add(imgDetectionLabel);</div><div class="line">        pane.add(framePanel, BorderLayout.CENTER);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keywordtype">void</span> update(Mat imgCapture, Mat imgThresh) {</div><div class="line">        imgCaptureLabel.setIcon(<span class="keyword">new</span> ImageIcon(HighGui.toBufferedImage(imgCapture)));</div><div class="line">        imgDetectionLabel.setIcon(<span class="keyword">new</span> ImageIcon(HighGui.toBufferedImage(imgThresh)));</div><div class="line">        frame.repaint();</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native OpenCV library</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line"></div><div class="line">        <span class="comment">// Schedule a job for the event dispatch thread:</span></div><div class="line">        <span class="comment">// creating and showing this application&#39;s GUI.</span></div><div class="line">        javax.swing.SwingUtilities.invokeLater(<span class="keyword">new</span> Runnable() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> run() {</div><div class="line">                <span class="keyword">new</span> ThresholdInRange(args);</div><div class="line">            }</div><div class="line">        });</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><p> The tutorial code's is shown lines below. You can also download it from <a href="https://github.com/opencv/opencv/tree/master/samples/python/tutorial_code/imgProc/threshold_inRange/threshold_inRange.py">here</a> </p><div class="fragment"><div class="line"><span class="keyword">from</span> __future__ <span class="keyword">import</span> print_function</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> argparse</div><div class="line"></div><div class="line">max_value = 255</div><div class="line">max_value_H = 360//2</div><div class="line">low_H = 0</div><div class="line">low_S = 0</div><div class="line">low_V = 0</div><div class="line">high_H = max_value_H</div><div class="line">high_S = max_value</div><div class="line">high_V = max_value</div><div class="line">window_capture_name = <span class="stringliteral">&#39;Video Capture&#39;</span></div><div class="line">window_detection_name = <span class="stringliteral">&#39;Object Detection&#39;</span></div><div class="line">low_H_name = <span class="stringliteral">&#39;Low H&#39;</span></div><div class="line">low_S_name = <span class="stringliteral">&#39;Low S&#39;</span></div><div class="line">low_V_name = <span class="stringliteral">&#39;Low V&#39;</span></div><div class="line">high_H_name = <span class="stringliteral">&#39;High H&#39;</span></div><div class="line">high_S_name = <span class="stringliteral">&#39;High S&#39;</span></div><div class="line">high_V_name = <span class="stringliteral">&#39;High V&#39;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>on_low_H_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_H</div><div class="line">    <span class="keyword">global</span> high_H</div><div class="line">    low_H = val</div><div class="line">    low_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_H-1, low_H)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(low_H_name, window_detection_name, low_H)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>on_high_H_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_H</div><div class="line">    <span class="keyword">global</span> high_H</div><div class="line">    high_H = val</div><div class="line">    high_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a6dff8b6e9105b6d817b493e7be157c90">max</a>(high_H, low_H+1)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(high_H_name, window_detection_name, high_H)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>on_low_S_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_S</div><div class="line">    <span class="keyword">global</span> high_S</div><div class="line">    low_S = val</div><div class="line">    low_S = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_S-1, low_S)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(low_S_name, window_detection_name, low_S)</div><div class="line"></div><div class="line"><span class="keyword">def </span>on_high_S_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_S</div><div class="line">    <span class="keyword">global</span> high_S</div><div class="line">    high_S = val</div><div class="line">    high_S = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a6dff8b6e9105b6d817b493e7be157c90">max</a>(high_S, low_S+1)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(high_S_name, window_detection_name, high_S)</div><div class="line"></div><div class="line"><span class="keyword">def </span>on_low_V_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_V</div><div class="line">    <span class="keyword">global</span> high_V</div><div class="line">    low_V = val</div><div class="line">    low_V = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_V-1, low_V)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(low_V_name, window_detection_name, low_V)</div><div class="line"></div><div class="line"><span class="keyword">def </span>on_high_V_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_V</div><div class="line">    <span class="keyword">global</span> high_V</div><div class="line">    high_V = val</div><div class="line">    high_V = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a6dff8b6e9105b6d817b493e7be157c90">max</a>(high_V, low_V+1)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(high_V_name, window_detection_name, high_V)</div><div class="line"></div><div class="line">parser = argparse.ArgumentParser(description=<span class="stringliteral">&#39;Code for Thresholding Operations using inRange tutorial.&#39;</span>)</div><div class="line">parser.add_argument(<span class="stringliteral">&#39;--camera&#39;</span>, help=<span class="stringliteral">&#39;Camera divide number.&#39;</span>, default=0, type=int)</div><div class="line">args = parser.parse_args()</div><div class="line"></div><div class="line"></div><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(args.camera)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_capture_name)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_detection_name)</div><div class="line"></div><div class="line"></div><div class="line"></div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_H_name, window_detection_name , low_H, max_value_H, on_low_H_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_H_name, window_detection_name , high_H, max_value_H, on_high_H_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_S_name, window_detection_name , low_S, max_value, on_low_S_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_S_name, window_detection_name , high_S, max_value, on_high_S_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_V_name, window_detection_name , low_V, max_value, on_low_V_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_V_name, window_detection_name , high_V, max_value, on_high_V_thresh_trackbar)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordflow">while</span> <span class="keyword">True</span>:</div><div class="line">    </div><div class="line">    ret, frame = cap.read()</div><div class="line">    <span class="keywordflow">if</span> frame <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">        <span class="keywordflow">break</span></div><div class="line"></div><div class="line">    frame_HSV = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame, cv.COLOR_BGR2HSV)</div><div class="line">    frame_threshold = <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv.inRange</a>(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))</div><div class="line">    </div><div class="line"></div><div class="line">    </div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_capture_name, frame)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_detection_name, frame_threshold)</div><div class="line">    </div><div class="line"></div><div class="line">    key = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(30)</div><div class="line">    <span class="keywordflow">if</span> key == ord(<span class="stringliteral">&#39;q&#39;</span>) <span class="keywordflow">or</span> key == 27:</div><div class="line">        <span class="keywordflow">break</span></div></div><!-- fragment -->  </div> <h2>Explanation </h2>
<p>Let's check the general structure of the program:</p><ul>
<li>Capture the video stream from default or supplied capturing device.</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    VideoCapture cap(argc &gt; 1 ? atoi(argv[1]) : 0);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        cap = <span class="keyword">new</span> VideoCapture(cameraDevice);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">cap = <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">cv.VideoCapture</a>(args.camera)</div></div><!-- fragment --> </div> <ul>
<li>Create a window to display the default frame and the threshold frame.</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(window_capture_name);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>(window_detection_name);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="comment">// Create and set up the window.</span></div><div class="line">        frame = <span class="keyword">new</span> JFrame(WINDOW_NAME);</div><div class="line">        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</div><div class="line">        frame.addWindowListener(<span class="keyword">new</span> WindowAdapter() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> windowClosing(WindowEvent windowEvent) {</div><div class="line">                captureTask.cancel(<span class="keyword">true</span>);</div><div class="line">            }</div><div class="line">        });</div><div class="line">        <span class="comment">// Set up the content pane.</span></div><div class="line">        Image img = HighGui.toBufferedImage(matFrame);</div><div class="line">        addComponentsToPane(frame.getContentPane(), img);</div><div class="line">        <span class="comment">// Use the content pane&#39;s default BorderLayout. No need for</span></div><div class="line">        <span class="comment">// setLayout(new BorderLayout());</span></div><div class="line">        <span class="comment">// Display the window.</span></div><div class="line">        frame.pack();</div><div class="line">        frame.setVisible(<span class="keyword">true</span>);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_capture_name)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_detection_name)</div></div><!-- fragment --> </div> <ul>
<li>Create the trackbars to set the range of HSV values</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="comment">// Trackbars to set thresholds for HSV values</span></div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low H&quot;</span>, window_detection_name, &amp;low_H, max_value_H, on_low_H_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High H&quot;</span>, window_detection_name, &amp;high_H, max_value_H, on_high_H_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low S&quot;</span>, window_detection_name, &amp;low_S, max_value, on_low_S_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High S&quot;</span>, window_detection_name, &amp;high_S, max_value, on_high_S_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;Low V&quot;</span>, window_detection_name, &amp;low_V, max_value, on_low_V_thresh_trackbar);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">createTrackbar</a>(<span class="stringliteral">&quot;High V&quot;</span>, window_detection_name, &amp;high_V, max_value, on_high_V_thresh_trackbar);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_H_NAME));</div><div class="line">        sliderLowH = <span class="keyword">new</span> JSlider(0, MAX_VALUE_H, 0);</div><div class="line">        sliderLowH.setMajorTickSpacing(50);</div><div class="line">        sliderLowH.setMinorTickSpacing(10);</div><div class="line">        sliderLowH.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowH.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowH);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_H_NAME));</div><div class="line">        sliderHighH = <span class="keyword">new</span> JSlider(0, MAX_VALUE_H, MAX_VALUE_H);</div><div class="line">        sliderHighH.setMajorTickSpacing(50);</div><div class="line">        sliderHighH.setMinorTickSpacing(10);</div><div class="line">        sliderHighH.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighH.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighH);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_S_NAME));</div><div class="line">        sliderLowS = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderLowS.setMajorTickSpacing(50);</div><div class="line">        sliderLowS.setMinorTickSpacing(10);</div><div class="line">        sliderLowS.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowS.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowS);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_S_NAME));</div><div class="line">        sliderHighS = <span class="keyword">new</span> JSlider(0, MAX_VALUE, MAX_VALUE);</div><div class="line">        sliderHighS.setMajorTickSpacing(50);</div><div class="line">        sliderHighS.setMinorTickSpacing(10);</div><div class="line">        sliderHighS.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighS.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighS);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(LOW_V_NAME));</div><div class="line">        sliderLowV = <span class="keyword">new</span> JSlider(0, MAX_VALUE, 0);</div><div class="line">        sliderLowV.setMajorTickSpacing(50);</div><div class="line">        sliderLowV.setMinorTickSpacing(10);</div><div class="line">        sliderLowV.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderLowV.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderLowV);</div><div class="line"></div><div class="line">        sliderPanel.add(<span class="keyword">new</span> JLabel(HIGH_V_NAME));</div><div class="line">        sliderHighV = <span class="keyword">new</span> JSlider(0, MAX_VALUE, MAX_VALUE);</div><div class="line">        sliderHighV.setMajorTickSpacing(50);</div><div class="line">        sliderHighV.setMinorTickSpacing(10);</div><div class="line">        sliderHighV.setPaintTicks(<span class="keyword">true</span>);</div><div class="line">        sliderHighV.setPaintLabels(<span class="keyword">true</span>);</div><div class="line">        sliderPanel.add(sliderHighV);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_H_name, window_detection_name , low_H, max_value_H, on_low_H_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_H_name, window_detection_name , high_H, max_value_H, on_high_H_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_S_name, window_detection_name , low_S, max_value, on_low_S_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_S_name, window_detection_name , high_S, max_value, on_high_S_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(low_V_name, window_detection_name , low_V, max_value, on_low_V_thresh_trackbar)</div><div class="line"><a class="code" href="../../d7/dfc/group__highgui.html#gaf78d2155d30b728fc413803745b67a9b">cv.createTrackbar</a>(high_V_name, window_detection_name , high_V, max_value, on_high_V_thresh_trackbar)</div></div><!-- fragment --> </div> <ul>
<li>Until the user want the program to exit do the following</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        cap &gt;&gt; frame;</div><div class="line">        <span class="keywordflow">if</span>(frame.empty())</div><div class="line">        {</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="comment">// Convert from BGR to HSV colorspace</span></div><div class="line">        <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(frame, frame_HSV, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0aa4a7f0ecf2e94150699e48c79139ee12">COLOR_BGR2HSV</a>);</div><div class="line">        <span class="comment">// Detect the object based on HSV Range Values</span></div><div class="line">        <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">inRange</a>(frame_HSV, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(low_H, low_S, low_V), <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(high_H, high_S, high_V), frame_threshold);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">    <span class="keyword">private</span> <span class="keyword">class </span>CaptureTask <span class="keyword">extends</span> SwingWorker&lt;Void, Mat&gt; {</div><div class="line">        @Override</div><div class="line">        <span class="keyword">protected</span> Void doInBackground() {</div><div class="line">            Mat matFrame = <span class="keyword">new</span> Mat();</div><div class="line"></div><div class="line">            <span class="keywordflow">while</span> (!isCancelled()) {</div><div class="line">                <span class="keywordflow">if</span> (!cap.read(matFrame)) {</div><div class="line">                    <span class="keywordflow">break</span>;</div><div class="line">                }</div><div class="line">                publish(matFrame.clone());</div><div class="line">            }</div><div class="line">            <span class="keywordflow">return</span> null;</div><div class="line">        }</div><div class="line"></div><div class="line">        @Override</div><div class="line">        <span class="keyword">protected</span> <span class="keywordtype">void</span> process(List&lt;Mat&gt; frames) {</div><div class="line">            Mat frame = frames.get(frames.size() - 1);</div><div class="line">            Mat frameHSV = <span class="keyword">new</span> Mat();</div><div class="line">            Imgproc.cvtColor(frame, frameHSV, Imgproc.COLOR_BGR2HSV);</div><div class="line">            Mat thresh = <span class="keyword">new</span> Mat();</div><div class="line">            Core.inRange(frameHSV, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(sliderLowH.getValue(), sliderLowS.getValue(), sliderLowV.getValue()),</div><div class="line">                    <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(sliderHighH.getValue(), sliderHighS.getValue(), sliderHighV.getValue()), thresh);</div><div class="line">            update(frame, thresh);</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    ret, frame = cap.read()</div><div class="line">    <span class="keywordflow">if</span> frame <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">        <span class="keywordflow">break</span></div><div class="line"></div><div class="line">    frame_HSV = <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv.cvtColor</a>(frame, cv.COLOR_BGR2HSV)</div><div class="line">    frame_threshold = <a class="code" href="../../d2/de8/group__core__array.html#ga48af0ab51e36436c5d04340e036ce981">cv.inRange</a>(frame_HSV, (low_H, low_S, low_V), (high_H, high_S, high_V))</div></div><!-- fragment --> </div> <ul>
<li>Show the images</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">        <span class="comment">// Show the frames</span></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(window_capture_name, frame);</div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(window_detection_name, frame_threshold);</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        imgCaptureLabel.setIcon(<span class="keyword">new</span> ImageIcon(HighGui.toBufferedImage(imgCapture)));</div><div class="line">        imgDetectionLabel.setIcon(<span class="keyword">new</span> ImageIcon(HighGui.toBufferedImage(imgThresh)));</div><div class="line">        frame.repaint();</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_capture_name, frame)</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_detection_name, frame_threshold)</div></div><!-- fragment --> </div> <ul>
<li>For a trackbar which controls the lower range, say for example hue value:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_low_H_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    low_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_H-1, low_H);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;Low H&quot;</span>, window_detection_name, low_H);</div><div class="line">}</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        sliderLowH.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valH = Math.min(sliderHighH.getValue()-1, source.getValue());</div><div class="line">                sliderLowH.setValue(valH);</div><div class="line">            }</div><div class="line">        });</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">def </span>on_low_H_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_H</div><div class="line">    <span class="keyword">global</span> high_H</div><div class="line">    low_H = val</div><div class="line">    low_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_H-1, low_H)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(low_H_name, window_detection_name, low_H)</div></div><!-- fragment --> </div>  <div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_low_H_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    low_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#ac22d7d32f1ec91fd106760a98a6f4731">min</a>(high_H-1, low_H);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;Low H&quot;</span>, window_detection_name, low_H);</div><div class="line">}</div></div><!-- fragment --><ul>
<li>For a trackbar which controls the upper range, say for example hue value:</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_high_H_thresh_trackbar(<span class="keywordtype">int</span>, <span class="keywordtype">void</span> *)</div><div class="line">{</div><div class="line">    high_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a6dff8b6e9105b6d817b493e7be157c90">max</a>(high_H, low_H+1);</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">setTrackbarPos</a>(<span class="stringliteral">&quot;High H&quot;</span>, window_detection_name, high_H);</div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        sliderHighH.addChangeListener(<span class="keyword">new</span> ChangeListener() {</div><div class="line">            @Override</div><div class="line">            <span class="keyword">public</span> <span class="keywordtype">void</span> stateChanged(ChangeEvent e) {</div><div class="line">                JSlider source = (JSlider) e.getSource();</div><div class="line">                <span class="keywordtype">int</span> valH = Math.max(source.getValue(), sliderLowH.getValue()+1);</div><div class="line">                sliderHighH.setValue(valH);</div><div class="line">            }</div><div class="line">        });</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line"><span class="keyword">def </span>on_high_H_thresh_trackbar(val):</div><div class="line">    <span class="keyword">global</span> low_H</div><div class="line">    <span class="keyword">global</span> high_H</div><div class="line">    high_H = val</div><div class="line">    high_H = <a class="code" href="../../d1/d10/classcv_1_1MatExpr.html#a6dff8b6e9105b6d817b493e7be157c90">max</a>(high_H, low_H+1)</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga67d73c4c9430f13481fd58410d01bd8d">cv.setTrackbarPos</a>(high_H_name, window_detection_name, high_H)</div></div><!-- fragment --> </div> <ul>
<li>It is necessary to find the maximum and minimum value to avoid discrepancies such as the high value of threshold becoming less than the low value.</li>
</ul>
<h2>Results </h2>
<ul>
<li>After compiling this program, run it. The program will open two windows</li>
<li><p class="startli">As you set the range values from the trackbar, the resulting frame will be visible in the other window.</p>
<div class="image">
<img src="../../Threshold_inRange_Tutorial_Result_input.jpeg" alt="Threshold_inRange_Tutorial_Result_input.jpeg"/>
</div>
 <div class="image">
<img src="../../Threshold_inRange_Tutorial_Result_output.jpeg" alt="Threshold_inRange_Tutorial_Result_output.jpeg"/>
</div>
 </li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:36 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
